package com.platform.hjt.erp.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.hjt.tools.dao.Page;
import com.hjt.tools.dao.PropertyFilter;
import com.hjt.tools.date.DateCreator;
import com.platform.hjt.base.controller.ReturnBody;
import com.platform.hjt.base.costant.StateValue;
import com.platform.hjt.base.service.BaseService;
import com.platform.hjt.erp.dao.ErpStorageFlowDAO;
import com.platform.hjt.erp.dao.ErpStorageFlowDetailDAO;
import com.platform.hjt.erp.model.ErpStock;
import com.platform.hjt.erp.model.ErpStorageFlow;
import com.platform.hjt.erp.model.ErpStorageFlowDetail;
import com.platform.hjt.flow.service.SnakerEngineFacets;
import com.platform.hjt.from.dao.FromQjOrderDetailDAO;
import com.platform.hjt.from.model.FromQjOrderDetail;

@Service
public class ErpStorageFlowService extends BaseService{

	@Resource
	private ErpStorageFlowDAO erpStorageFlowDAO;
	@Resource
	private ErpStorageFlowDetailDAO erpStorageFlowDetailDAO;
	@Resource
	private ErpStockService erpStockService;
	@Resource
	private SnakerEngineFacets facets;
	@Resource
	private FromQjOrderDetailDAO fromQjOrderDetailDAO;
	
	public ReturnBody saveErpStorageFlow(ErpStorageFlow erpStorageFlow){
		if(StringUtils.isNotBlank(erpStorageFlow.getStorageFlowId())){
			erpStorageFlowDAO.updatePerproties(erpStorageFlow);
			return  new ReturnBody(true, "更新成功");
		}else{
			
			erpStorageFlow.setCreateTime(DateCreator.getCurrentTime());
			erpStorageFlow.setSysCompanyInfo(getSessionSysCompanyInfo());
			erpStorageFlow.setSysUserInfo(getSessionUserInfo());
			erpStorageFlow.setIsDel(false);
			erpStorageFlowDAO.saveJ(erpStorageFlow);
			
			for(ErpStorageFlowDetail detail : erpStorageFlow.getDetailList()){
				detail.setErpStorageFlow(erpStorageFlow);
				erpStorageFlowDetailDAO.saveJ(detail);
				
				//库存整理
				ErpStock es = new ErpStock();
				es.setErpGoodInfo(detail.getErpGoodInfo());
				es.setErpStorage(erpStorageFlow.getErpStorage());
				es.setGoodCount(detail.getCount());
				erpStockService.saveErpStock(es, erpStorageFlow.getStorageFlowType());
			}
			
			return  new ReturnBody(true, "添加成功");
		}
	}
	
	public ReturnBody saveErpStorageFlowP(ErpStorageFlow erpStorageFlow, String processId, String orderId, String taskId){
		
		String status = getRequest().getParameter("status");
		if(status.equals("-1")){	//扭转上节点
			facets.getEngine().executeAndJumpTask(taskId, getSessionUserInfo().getSysUserInfoId(), null, null);
			return new ReturnBody(true, "流程已回退");
			
		} else if(status.equals("1")){
			erpStorageFlow.setCreateTime(DateCreator.getCurrentTime());
			erpStorageFlow.setSysCompanyInfo(getSessionSysCompanyInfo());
			erpStorageFlow.setSysUserInfo(getSessionUserInfo());
			erpStorageFlow.setIsDel(false);
			erpStorageFlow.setProcessId(processId);
			erpStorageFlow.setProcessOrderId(orderId);
			erpStorageFlow.setTaskId(taskId);
			erpStorageFlowDAO.saveJ(erpStorageFlow);
			
			for(ErpStorageFlowDetail detail : erpStorageFlow.getDetailList()){
				detail.setErpStorageFlow(erpStorageFlow);
				erpStorageFlowDetailDAO.saveJ(detail);
				
				//库存整理
				ErpStock es = new ErpStock();
				es.setErpGoodInfo(detail.getErpGoodInfo());
				es.setErpStorage(erpStorageFlow.getErpStorage());
				es.setGoodCount(detail.getCount());
				es.setColor(detail.getColor());
				erpStockService.saveErpStock(es, erpStorageFlow.getStorageFlowType());
				
				//入库
				if(erpStorageFlow.getStorageFlowType().equals(StateValue.InStorage.getKey())){
					String selectSql = "select t.* from  from_qj_order_detail t, from_qj_order o  where t.qj_order_id=o.qj_order_id and t.erp_good_info_id = ? and t.color = ? and o.process_id=? and o.process_order_id=?";
					if(fromQjOrderDetailDAO.createSQLQuery(selectSql, detail.getErpGoodInfo().getErpGoodInfoId(), detail.getColor(), processId, orderId).addEntity(FromQjOrderDetail.class).list().size() > 0){
						String updateSql = "update from_qj_order_detail t, from_qj_order o set t.is_in = true where t.qj_order_id=o.qj_order_id and t.erp_good_info_id = ? and t.color = ? and o.process_id=? and o.process_order_id=?";
						fromQjOrderDetailDAO.createSQLQuery(updateSql, detail.getErpGoodInfo().getErpGoodInfoId(), detail.getColor(), processId, orderId).executeUpdate();
					}
				} else 
					//出库
					if(erpStorageFlow.getStorageFlowType().equals(StateValue.OutStorage.getKey())){
						String selectSql = "select t.* from  from_qj_order_detail t, from_qj_order o  where t.qj_order_id=o.qj_order_id and t.erp_good_info_id = ? and t.color = ? and o.process_id=? and o.process_order_id=?";
						if(fromQjOrderDetailDAO.createSQLQuery(selectSql, detail.getErpGoodInfo().getErpGoodInfoId(), detail.getColor(), processId, orderId).addEntity(FromQjOrderDetail.class).list().size() > 0){
							String updateSql = "update from_qj_order_detail t, from_qj_order o set t.is_out = true where t.qj_order_id=o.qj_order_id and t.erp_good_info_id = ? and t.color = ? and o.process_id=? and o.process_order_id=?";
							fromQjOrderDetailDAO.createSQLQuery(updateSql, detail.getErpGoodInfo().getErpGoodInfoId(), detail.getColor(), processId, orderId).executeUpdate();
						}
					}
			}
			facets.getEngine().executeTask(taskId, getSessionUserInfo().getSysUserInfoId());
		}
		
		return  new ReturnBody(true, "操作成功");
	}
	
	public ReturnBody delErpStorageFlow(ErpStorageFlow erpStorageFlow){
		erpStorageFlow.setIsDel(true);
		erpStorageFlowDAO.updatePerproties(erpStorageFlow);
		return  new ReturnBody(true, "删除成功");
	}
	
	public Page<ErpStorageFlow> getErpStorageFlowList(Page<ErpStorageFlow> page, List<PropertyFilter> propertyFilterList){
		return erpStorageFlowDAO.findPage(page, propertyFilterList);
	}
	
	public List<ErpStorageFlow> getErpStorageFlowList(List<PropertyFilter> filters){
		return erpStorageFlowDAO.find(filters);
	}
	
	public ErpStorageFlow getErpStorageFlow(List<PropertyFilter> filters){
		return erpStorageFlowDAO.findUniqueBy(filters);
	}
	
	public ErpStorageFlow getErpStorageFlow(String propertyName, Object value){
		return erpStorageFlowDAO.findUniqueBy(propertyName, value);
	}
	
	public ErpStorageFlow getErpStorageFlow(Map<String, Object> map){
		return erpStorageFlowDAO.findUniqueBy(map);
	}
	
}
